======================================================================
"A 57600 Baud Interface" by Don Lancaster - ASK THE GURU - April, 1989
----------------------------------------------------------------------
  Edited 2017 (for personal use) by: James Davis, Beagle Buddy #227.
======================================================================

Show me an honest 57600 Baud Serial Interface.

I have long been disappointed with the dismal IIgs serial interface 
firmware and its software support.  First, the IIgs communicates much 
slower than the IIe.  For instance, a IIe takes only 6 microseconds to 
find out if a character is ready to be read.  That IIgs takes 136 
microseconds or so to do the same task in its fast mode.

Second, that IIgs interface is incompatible with pretty near all of 
the important earlier IIe software, so you have to substitute a Super 
Serial Card instead if you are to reuse many of your older but better 
routines.  And, third, we still are lacking essential print drivers 
for plain old Send-PS and even a properly working Epson driver.  This 
is inexcusable.

So, step number one is to flush all of your internal IIgs serial 
firmware, and substitute a Super Serial Card.  This at least will get 
you up to a compatible 19200 baud, and your de-facto baud rates can 
end up in the 16000 range, at least when you are using AppleWriter.

But, I wanted and needed much more than this.  Just how fast can an 
unassisted IIe or IIgs output serial data? The suprising answer to 
this is 14 Megabaud.  Yeah.  Mega, not kilo.  You do this by putting 
your data on the super HIRES screen and using the video connector for 
the serial output.  To do so, though, would be socially irresponsible, 
because of the extreme chagrin and stress this would cause in the Mac 
and 386 communities.

Instead, lets just be content to plod along at a leisurely but honest 
57600 baud.  That will still end up ridiculously faster than AppleTalk 
with its heavy software overhead, and much faster than most anything 
else available on the Mac or a 386 machine.  And it opens up some of 
the really exciting speedup tricks that we will look at next month.

For now, we will use a transmit-only simplex mode using hard wire DTR 
handshaking.  You can drop on down to 19200 baud with your super 
serial card anytime you really need a full two-way communications 
channel.

The process works just fine on a II+, IIe, or IIgs.  You use the game 
port  output as a RS-232 or RS-423 driver.  One possible cable 
schematic appears in figure one.  The maximum recommended cable length 
is five feet, unless you add suitable hardware drivers.  Plans are 
underfoot to offer this cable or cable kits through the folks at 
Redmound Cable.  Give them a call for more details.

======================================================================
                                Page 1
======================================================================
"A 57600 Baud Interface" by Don Lancaster - ASK THE GURU - April, 1989
======================================================================

Fig. 1 - Apple II game port to RS-232 serial adaptor cable:
----------------------------------------------------------------------
NOTE: Outputs are "unbalanced" at 0 and +3 volts.  The max recomended 
length is five feet.  For longer runs and true bipolar "balanced" RS-
232, add a single chip and single +5v supply driver such as a Maxim 
MAX-232.  (*1: Use only one of the four D-sub connectors shown below.)

       +---------------------------------+ data
    TxD|15                               | out
+--O---O---O---O---O---O---O---O--+      |
| NC  AN0 AN1 AN2 AN3 PD3 PD1 SW3 |      |      Female DIP socket on
 ]     GAME I/O   ___             |      |      Apple II motherboard
| +5V SW0 SW1 SW2 STB PD0 PD2 GND |      |
+--O---O---O---O---O---O---O---O--+      |      You can use a 4-wire
   |1          |4              |8        |      telephone cable up to
   |   _____   |busy           |SG       |      5 ft long, or a longer
   +--| 22K |--+(Shift-Keys)   |(black)  |      shielded cable pigtail
   |      |               |         |      up to 10 feet long.
  ||         ||              |        ||
  |R|         |R|              |        |R| <-- These three Resistors 
  |_|         |_|              |        |_|     are each 470 ohms 
  a|          b|              c|        d|     
+--|-----------+---------------|---------|-------------+ DTR (yellow)
|  |             +-------------+---------|-----------+ |
|  +-----------+-|-+---------------------|-----------|-|-----+ DSR+DCD
|  DSR+DCD     | | |     +---------------+-----------|-|---+ |  (red)
|   (red)   DCD| | |DSR  |RxD (green)                | |   | +-------+
|              |8|7|6    |3                          |5|4  |2|1 DCD  |
|  ,-----------|-|-|-----|-----.                   ,-|-|---|-|-.     |
|  \ O O O O O O O O O O O O O / DB-25        DE-9 \ O O O O O /     |
|   \ O O O O O O O O O O O O /                     \ O O O O /      |
|    `----------|------------ Connectors are female `------|       |
|DTR (yellow)   |20            sockets on cable ends        |6 DSR   |
+---------------+        (Mount *1 on Apple II back panel)  +--------+

  a|DSR+DCD   b|DTR           c|SG      d|RxD  
   |       +---+---------------|---------|---------------------------+
   |       | +-----------------+---------|----------+                |
+--+-+-----|-|---------------------------|--------+-|-+              |
|    | +---|-|---------------------------+--+  DSR| | |DCD           |
| DCD|1|2  |4|5 SG                       RxD|3    |6|7|8             |
|  ,-|-|---|-|-.                      ,-----|-----|-|-|-----------.  |
|  \ O O O O O / DE-9           DB-25 \ O O O O O O O O O O O O O /  |
|   \ O O O O /                        \ O O O O O O O O O O O O /   |
|    `|------ Connectors are pigtails, `------------|----------    |
|  DSR|6       male plugs on cable ends            20|DTR            |
+-----+       (Attach *1 to socket on PC)            +---------------+

======================================================================
                                Page 2
======================================================================
"A 57600 Baud Interface" by Don Lancaster - ASK THE GURU - April, 1989
======================================================================

Source code for a suitable software driver appears in figure two.  
Annunciator AN0 is used for data out, since reset of a IIe or IIgs 
drives AN0 and AN1 low and AN2 and AN3 high.  The switch input at SW2 
is used to avoid any conflicts with the open-apple and closed-apple 
keys.  [SW2 conflicts with the shift key mod on newer Apple IIe's.]

You also do have the option of hearing the data as it is output.  This 
can be handy for debug and speed testing.  Details on this are 
different between the IIe and IIgs.  In general, omitting the speaker 
whapping will turn the sound on on a IIe and off on a IIgs.  And 
including that speaker whapping will turn the sound on a IIe off and 
on on a IIgs.  This has to do with the IIgs speed byte at $C036 
unintentionally whapping the speaker on a IIe.  Arrghhh.

I chose to use brute force code so that two of the data bits could be 
a microsecond shorter than the others.  This was needed for the most 
precise possible baud rate timing.  The code is fully equalized and 
will produce negligible jitter.  Note that the IIgs is forced into its 
slow mode during any character output times.

Fig. 2 - EDASM source code for a 57600 baud serial driver:
----------------------------------------------------------------------

----- NEXT OBJECT FILE NAME IS BAUD.57600

0300:        0300   3           ORG $0300  ; usual place

0300:               5 ;
0300:               6 ; ***********************************
0300:               7 ; *                                 *
0300:               8 ; * 57600 Baud Print Driver         *
0300:               9 ; * for Apple IIe and IIgs          *
0300:              10 ; *                                 *
0300:              11 ; * Version 1.0 ($0300-03AE)        *
0300:              12 ; *                                 *
0300:              13 ; * 1-20-89 [Edited* by JPD 4/2017] *
0300:              14 ; *.................................*
0300:              15 ; *                                 *
0300:              16 ; * Copyright c 1989 by             *
0300:              17 ; *                                 *
0300:              18 ; * Don Lancaster and Synergetics   *
0300:              19 ; * Box 809, Thatcher, AZ 85552     *
0300:              20 ; * (602) 428-4073                  *
0300:              21 ; *                                 *
0300:              22 ; * All commercial rights reserved. *
0300:              23 ; *                                 *
0300:              24 ; ***********************************

======================================================================
                                Page 3
======================================================================
"A 57600 Baud Interface" by Don Lancaster - ASK THE GURU - April, 1989
======================================================================

Fig. 2 - EDASM source code for a 57600 baud serial driver (continued):
----------------------------------------------------------------------
0300:              26 ; *** WHAT IT DOES ***

0300:              28 ; This module outputs serial data to the game
0300:              29 ; paddle ports at an honest 57600 baud. DTR

0300:              30 ; handshaking in a simplex mode (transmit only)
0300:              31 ; is used.  At present, a tone is provided to
0300:              32 ; trace handshaking activity.  An external
0300:              33 ; driver is needed for "true" bipolar RS-232.

0300:              35 ; *** HOW TO USE IT ***

0300:              37 ; To use, store the character to be output at
0300:              38 ; $0303 and JSR $0300.  From BASIC, POKE char
0300:              39 ; to 771 and then CALL 768.
0300:              40 ;
0300:              42 ; *** GOTCHAS ***

0300:              44 ; Four game port pins are needed:
0300:              45 ;
0300:              46 ; Game port pin-8 (GND) connects to DB-25 pin-7 
0300:              47 ; or DB-9 pin-5 (SG)=(Signal Ground)
0300:              48 ;
0300:              49 ; Game port pin-15 (AN0) connects to DB-25 pin-3 
0300:              50 ; or DB-9 pin-2 (Data out) (RxD)=[DTE input] 
0300:              51 ;
0300:              52 ; Game port pin-4 (SW2) connects to DB-25 pin-20 
0300:              53 ; or DB-9 pin-4 (Busy) (DTR)=[DTE output] 
0300:              54 ;
0300:              55 ; Game port pin-1 (+5v) connects to DB-25 pin-6 
0300:              56 ; or DB-9 pin-6 (DSR)=[DTE input] & DB-25 pin-8
0300:              ++ ; or DB-9 pin-1 (DCD)=[DTE input]
0300:              57 ;
0300:              58 ; Data format is 7 data bits and one stop bit.

0300:              60 ; **** MAGIC NUMBERS ****

0300:              62 ; One data bit at 57600 baud equals 17.3611
0300:              63 ; microseconds.  The IIe or slow IIgs has a
0300:              64 ; clock of (3.579545/3.5)*(65/65.1428)=1.020485
0300:              65 ; Mhz, corresponding to a period of 0.9799
0300:              66 ; microseconds.  There are 17.3611/0.9799=17.71
0300:              67 ; clock cycles per baud bit.

0300:              69 ; start bit (18 cycles) end error = +0.29

======================================================================
                                Page 4
======================================================================
"A 57600 Baud Interface" by Don Lancaster - ASK THE GURU - April, 1989
======================================================================

Fig. 2 - EDASM source code for a 57600 baud serial driver (continued):
----------------------------------------------------------------------
0300:              70 ; bit #1    (17 cycles) end error = -0.42
0300:              71 ; bit #2    (18 cycles) end error = -0.13
0300:              72 ; bit #3    (18 cycles) end error = +0.16
0300:              73 ; bit #4    (18 cycles) end error = +0.45
0300:              74 ; bit #5    (17 cycles) end error = -0.26

0300:              75 ; bit #6    (18 cycles) end error = +0.03
0300:              76 ; bit #7    (18 cycles) end error - +0.32

0300:              78 ; **** HOOKS ****

0300:        C036  80 SPEED  EQU $C036  ; Speed setting byte
0300:        C059  81 SETBIT EQU $C059  ; Make output high
0300:        C058  82 CLRBIT EQU $C058  ; Make output low
0300:        C063  83 BUSY   EQU $C063  ; Handshaking input
0300:        C030  84 NOISE  EQU $C030  ; Click speaker

0300:              86 ; *** MAIN 57600 CODE ***

0300:4C 04 03      88 LINK   JMP B57600 ; Bridge over BASIC
0303:FF            89 CHAR   DFB $FF    ; Character stash
0304:08            90 B57600 PHP        ; Save status
0305:78            91        SEI        ; No interrupts
0306:2C 58 C0      92        BIT CLRBIT ; affirm low output
0309:2C 63 C0      93 HS     BIT BUSY   ; Check handshaking
030C:10 FB   0309  94        BPL HS     ; Hang till handshake free
030E:2C 30 C0      95        BIT NOISE  ; Whap speaker once
0311:AD 36 C0      96        LDA SPEED  ; IIgs speed and save
0314:48            97        PHA        ; 
0315:29 7F         98        AND #$7F   ; 
0317:8D 36 C0      99        STA SPEED  ; Force slow speed
031A:AD 03 03     100        LDA CHAR   ; get character

031D:2C 59 C0     102        BIT SETBIT ; generate start bit
0320:48           103        PHA        ; stall for 18 - 11 = 7 cycles
0321:68           104        PLA        ; since 11 are used before

0322:6A           106 AX     ROR A      ; Get First Data Bit
0323:90 09   032E 107        BCC AL     ; and pick high/low
0325:B0 00   0327 108        BCS AH     ; stall for 3
0327:2C 58 C0     109 AH     BIT CLRBIT ; Low if a one
032A:B0 00   032C 110        BCS AH1    ; stall for 17-14 = 3
032C:B0 08   0336 111 AH1    BCS BX     ; and exit
032E:EA           112 AL     NOP        ; stall 2
032F:2C 59 C0     113        BIT SETBIT ; High if a zero

======================================================================
                                Page 5
======================================================================
"A 57600 Baud Interface" by Don Lancaster - ASK THE GURU - April, 1989
======================================================================

Fig. 2 - EDASM source code for a 57600 baud serial driver (continued):
----------------------------------------------------------------------
0332:90 00   0334 114        BCC AL2    ; stall for 17-14 = 3
0334:90 00   0336 115 AL2    BCC BX     ; and exit

0336:6A           117 BX     ROR A      ; Get Second Data Bit
0337:90 07   0340 118        BCC BL     ; and pick high/low

0339:B0 00   033B 119        BCS BH     ; 
033B:2C 58 C0     120 BH     BIT CLRBIT ; Low if a one
033E:B0 06   0346 121 BH1    BCS BY     ; and continue
0340:EA           122 BL     NOP        ; stall 2
0341:2C 59 C0     123        BIT SETBIT ; High if a zero
0344:90 00   0346 124 BL2    BCC BY     ; 
0346:EA           125 BY     NOP        ; stall for 18-14 = 4
0347:EA           126        NOP        ; 

0348:6A           128 CX     ROR A      ; Get Third Data Bit
0349:90 07   0352 129        BCC CL     ; and pick high/low
034B:B0 00   034D 130        BCS CH     ; 
034D:2C 58 C0     131 CH     BIT CLRBIT ; Low if a one
0350:B0 06   0358 132 CH1    BCS CY     ; and continue
0352:EA           133 CL     NOP        ; stall 2
0353:2C 59 C0     134        BIT SETBIT ; High if a zero
0356:90 00   0358 135 CL2    BCC CY     ; 
0358:EA           136 CY     NOP        ; stall for 18-14 = 4
0359:EA           137        NOP        ; 

035A:6A           139 DX     ROR A      ; Get Fourth Data Bit
035B:90 07   0364 140        BCC DL     ; and pick high/low
035D:B0 00   035F 141        BCS DH     ; 
035F:2C 58 C0     142 DH     BIT CLRBIT ; Low if a one
0362:B0 06   036A 143 DH1    BCS DY     ; and continue
0364:EA           144 DL     NOP        ; 
0365:2C 59 C0     145        BIT SETBIT ; High if a zero
0368:90 00   036A 146 DL2    BCC DY     ; 
036A:EA           147 DY     NOP        ; stall for 18-14 = 4
036B:EA           148        NOP        ; 

036C:6A           150 EX     ROR A      ; Get Fifth Data Bit
036D:90 09   0378 151        BCC EL     ; and pick high/low
036F:B0 00   0371 152        BCS EH     ; 
0371:2C 58 C0     153 EH     BIT CLRBIT ; Low if a one
0374:B0 00   0376 154        BCS EH1    ; stall for 17-14 = 3
0376:B0 08   0380 155 EH1    BCS FX     ; and exit
0378:EA           156 EL     NOP        ; stall 2
0379:2C 59 C0     157        BIT SETBIT ; High if a zero

======================================================================
                                Page 6
======================================================================
"A 57600 Baud Interface" by Don Lancaster - ASK THE GURU - April, 1989
======================================================================

Fig. 2 - EDASM source code for a 57600 baud serial driver (continued):
----------------------------------------------------------------------
037C:90 00   037E 158        BCC EL2    ; stall for 17-14 = 3
037E:90 00   0380 159 EL2    BCC FX     ; and exit

0380:6A           161 FX     ROR A      ; Get Sixth Data Bit
0381:90 07   038A 162        BCC FL     ; and pick high/low

0383:B0 00   0385 163        BCS FH     ; 
0385:2C 58 C0     164 FH     BIT CLRBIT ; Low if a one
0388:B0 06   0390 165 FH1    BCS FY     ; and continue
038A:EA           166 FL     NOP        ; stall 2
038B:2C 59 C0     167        BIT SETBIT ; High if a zero
038E:90 00   0390 168 FL2    BCC FY     ; 
0390:EA           169 FY     NOP        ; stall for 18-14 = 4
0391:EA           170        NOP        ; 

0392:6A           172 GX     ROR A      ; Get Seventh Data Bit
0393:90 07   039C 173        BCC GL     ; and pick high/low
0395:B0 00   0397 174        BCS GH     ; 
0397:2C 58 C0     175 GH     BIT CLRBIT ; Low if a one
039A:B0 06   03A2 176 GH1    BCS GY     ; and continue
039C:EA           177 GL     NOP        ; 
039D:2C 59 C0     178        BIT SETBIT ; High if a zero
03A0:90 00   03A2 179 GL2    BCC GY     ; 
03A2:48           180 GY     PHA        ; stall for 18-7 = 11
03A3:68           181        PLA        ; 
03A4:EA           182        NOP        ; 
03A5:EA           183        NOP        ; 
03A6:2C 58 C0     184        BIT CLRBIT ; Begin stop bits
03A9:68           185        PLA        ; get old speed back
03AA:8D 36 C0     186        STA SPEED  ; and restore
03AD:28           187        PLP        ; get old flags back
03AE:60           188        RTS        ; and exit as subroutine

SUCCESSFUL ASSEMBLY: NO ERRORS

----------------------------------------------------------------------

To use your driver, you connect your cable and install the machine 
language code, usually by BLOADing it.  From machine language, you 
store the character to be output in $0303 and then do a JSR $0300.  
Your new character will get output so long as the DTR busy line is not 
low.  If the busy line is low, the driver will wait till DTR goes high 
before continuing.  Note that a permanantly low DTR will hang your 
machine.


======================================================================
                                Page 7
======================================================================
"A 57600 Baud Interface" by Don Lancaster - ASK THE GURU - April, 1989
======================================================================

From BASIC, you just POKE your character to be sent to 771 and then do 
a CALL 768.  Naturally, there is a horrendous speed mismatch if you 
pair BASIC with 57600 baud, but this mode is very useful for 
preliminary testing and debugging.

Figure three does give you the manual hex dump for your final driver, 
if you are not now using an assembly system.


Fig. 3 - How to hand load your 57600 baud object code driver:
----------------------------------------------------------------------

1. Get into BASIC.SYSTEM and then CALL -151 to enter the monitor.
2. Enter the following code . . .

$0300: 4C 04 03 FF 08 78 2C 58 C0 2C 53 C0 10 FB 2C 30 <cr>
$0310: C0 AD 36 C0 48 29 7F 8D 36 C0 AD 03 03 2C 59 C0 <cr>
$0320: 48 68 6A 90 09 B0 00 2C 58 C0 B0 00 B0 08 EA 2C <cr>
$0330: 59 C0 90 00 90 00 6A 90 07 B0 00 2C 58 C0 B0 06 <cr>
$0340: EA 2C 59 C0 90 00 EA EA 6A 90 07 B0 00 2C 58 C0 <cr>
$0350: B0 06 EA 2C 59 C0 90 00 EA EA 6A 90 07 B0 00 2C <cr>
$0360: 58 C0 B0 06 EA 2C 59 C0 90 00 EA EA 6A 90 09 B0 <cr>
$0370: 00 2C 58 C0 B0 00 B0 08 EA 2C 59 C0 90 00 90 00 <cr>
$0380: 6A 90 07 B0 00 2C 58 C0 B0 06 EA 2C 59 C0 90 00 <cr>
$0390: EA EA 6A 90 07 B0 00 2C 58 C0 B0 06 EA 2C 59 C0 <cr>
$03A0: 90 00 48 68 EA EA 2C 58 C0 68 8D 36 C0 28 60 11 <cr>

3. Do a 300LLLL and compare the listing against figure two.
4. Do a BSAVE BAUD.57600, A$0300, L$AF to save the code to disk.
5. To install at run time, do a BLOAD BAUD.57600.

----------------------------------------------------------------------

The figure one circuit only works with modified RS-232 receivers that 
can accept 0 and +5 logic states as well as the usual -3 and +3.  You 
can also directly drive RS-423 with a connector change.  For a "true" 
RS-232 interface, you may want to add on a Maxim MAX232 driver chip.

Well see some examples of these alternatives in future issues.  We 
will also see a much fancier and far more powerful internal 
AppleWriter 57600 baud interface next month.

======================================================================





======================================================================
                                Page 8
======================================================================
    MAX232 - from Wikipedia, the free encyclopedia <wikipedia.org>
----------------------------------------------------------------------
  Edited 2017 (for personal use) by: James Davis, Beagle Buddy #227.
======================================================================

The MAX232 is an integrated circuit first created in 1987 by Maxim 
Integrated Products that converts signals from a TIA-232 (RS-232) 
serial port to signals suitable for use in TTL-compatible digital 
logic circuits. The MAX232 is a dual transmitter / dual receiver that 
typically is used to convert the RX, TX, CTS, RTS signals.

The drivers provide TIA-232 voltage level outputs (about 7.5 volts) 
from a single 5-volt supply by on-chip charge pumps and external 
capacitors. This makes it useful for implementing TIA-232 in devices 
that otherwise do not need any other voltages.

The receivers reduce TIA-232 inputs, which may be as high as 25 
volts, to standard 5 volt TTL levels. These receivers have a typical 
threshold of 1.3 volts and a typical hysteresis of 0.5 volts.

The MAX232 replaced an older pair of chips MC1488 and MC1489 that 
performed similar RS-232 translation. The MC1488 quad transmitter chip 
required 12 volt and -12 volt power, and MC1489 quad receiver chip 
required 5 volt power. The main disadvantages of this older solution 
was the +/- 12 volt power requirement, only supported 5 volt digital 
logic, and two chips instead of one.

Versions

The later MAX232A is backward compatible with the original MAX232 but 
may operate at higher baud rates and can use smaller external 
capacitors  0.1 ?F in place of the 1.0 ?F capacitors used with the 
original device. The newer MAX3232 and MAX3232E are also backwards 
compatible, but operates at a broader voltage range, from 3 to 5.5 V.

Pin-to-pin compatible versions from other manufacturers are ICL232, 
SP232, ST232, ADM232 and HIN232. Texas Instruments makes compatible 
chips, using MAX232 as the part number.

Voltage levels

It is helpful to understand what occurs to the voltage levels. When a 
MAX232 IC receives a TTL level to convert, it changes a TTL logic 0 to 
between +3 and +15 V, and changes TTL logic 1 to between ?3 and ?15 V, 
and vice versa for converting from TIA-232 to TTL. This can be 
confusing when you realize that the TIA-232 data transmission voltages 
at a certain logic state are opposite from the TIA-232 control line 
voltages at the same logic state. To clarify the matter, see the table 
below. For more information, see RS-232 voltage levels.


======================================================================
                                Page A1
======================================================================
    MAX232 - from Wikipedia, the free encyclopedia <wikipedia.org>
======================================================================

Applications

The MAX232(A) has two receivers that convert from RS-232 to TTL 
voltage levels, and two drivers that convert from TTL logic to RS-232 
voltage levels. As a result, only two out of all RS-232 signals can be 
converted in each direction. Typically, the first driver/receiver pair 
of the MAX232 is used for TX and RX signals, and the second one for 
CTS and RTS signals.

There are not enough drivers/receivers in the MAX232 to also connect 
the DTR, DSR, and DCD signals. Usually, these signals can be omitted 
when, for example, communicating with a PC's serial interface. If the 
DTE really requires these signals, either a second MAX232 is needed, 
or some other IC from the MAX232 family can be used. Also, it is 
possible to connect DTR (DE-9 pin #4) directly to DSR (DE-9 pin #6) 
without going through any circuitry, which provides an automatic 
(brain-dead) DSR acknowledgment of the incoming DTR signal.

Derivatives

The MAX232 family was subsequently extended by Maxim to versions with 
four receivers and transmitters (the MAX238) and a version with eight 
receivers and transmitters (the MAX248), as well as several other 
combinations of receivers and transmitters [Maxim MAX220-MAX249].


======================================================================





















======================================================================
                                Page A2
